﻿using Xejen.Communication.Socket.Abstract;
using Xejen.Communication.Socket.Configuration;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Net;
using System.Text;

namespace Xejen.Communication.Socket
{
    /// <summary>
    /// 创建以<see langword="TcpClient"/>及<see langword="TcpListener"/>为驱动的Socket请求驱动器
    /// </summary>
    /// <creator>marc</creator>
    internal class TcpInvokerCreator : ISocketInvokerCreator<TcpInvoker>
    {
        private readonly ConcurrentDictionary<ConnectionConfig, TcpInvoker> _dictionary = new ConcurrentDictionary<ConnectionConfig, TcpInvoker>();
      
        /// <inheritdoc/>
        public TcpInvoker Create(ConnectionConfig config)
        {
            TcpInvoker request = new TcpInvoker(config);
            return request;
        }

        /// <inheritdoc/>
        public TcpInvoker CreateOrGet(ConnectionConfig config)
        {
            return _dictionary.GetOrAdd(config, (setting) =>
            {
                TcpInvoker request = new TcpInvoker(config);
                return request;
            });
        }
    }
}
